package com.jsz.peini.redis.util;

import java.util.Date;
import java.util.LinkedHashMap;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.jsz.peini.bean.square.SquareComment;
import com.jsz.peini.bean.square.SquareImage;
import com.jsz.peini.bean.square.SquareInfo;
import com.jsz.peini.bean.user.UserInfo;
import com.jsz.peini.common.util.DateTimeUtils;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.common.util.SpringContextUtil;
import com.jsz.peini.mapper.user.UserInfoMapper;
import com.jsz.peini.redis.base.RedisConstants;
import com.jsz.peini.redis.base.RedisUtil;
import com.jsz.peini.redis.base.SerializeUtil;
import com.jsz.peini.vo.SquareCommtVo;
import com.jsz.peini.vo.SquareImageVo;
import com.jsz.peini.vo.SquareVo;

/**
 * @author Han Guohong
 * @version 2017-4-15 下午3:30:22
 * 广场缓存操作类
 */
public class SquareRedisUtil {
	
	/**
	 * 将广场信息放入redis缓存
	 * @author Han Guohong
	 * @param squareVo
	 */
	public static void addInfoToRedis(SquareInfo squareInfo,String userId){
		UserInfoMapper userInfoMapper = (UserInfoMapper)SpringContextUtil.getBean(UserInfoMapper.class);
		UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
		
		SquareVo squareVo = new SquareVo();
		squareVo.setId(squareInfo.getId().toString());
		squareVo.setAddress(squareInfo.getAddress());
		squareVo.setContent(squareInfo.getContent());
		squareVo.setImageHead(StringUtils.trimToEmpty(userInfo.getImageHead()));
		squareVo.setNickName(StringUtils.trimToEmpty(PeiniUtils.updateNickName(userInfo.getNickname(),userInfo.getAccCode())));
		squareVo.setSquareTime(DateTimeUtils.formatDateTime2String(new Date(), "yyyy-MM-dd HH:mm"));
		squareVo.setUserId(userId);
		
		RedisUtil.lpush(RedisConstants.SQUARE_LIST.getBytes(), SerializeUtil.serialize(squareVo));
	}
	
	/**
	 * 将广场图片写入缓存
	 * @author Han Guohong
	 * @param squareId
	 * @param squareImage
	 */
	public static void setSquareImage(String squareId,SquareImage squareImage){
		SquareImageVo imageVo = new SquareImageVo();
		imageVo.setId(squareImage.getId().toString());
		imageVo.setImageSrc(squareImage.getImageSrc());
		imageVo.setImageSmall(squareImage.getImageSmall());
		
		RedisUtil.hset((RedisConstants.SQUARE_IMAGE+squareId).getBytes(), squareImage.getId().toString().getBytes(), SerializeUtil.serialize(imageVo));
	}
	
	public static void setSquareCommt(String squareId,LinkedHashMap<String, Object> linkedHashMap){
		SquareCommtVo vo = new SquareCommtVo();
		String mapUser = linkedHashMap.get("userId").toString();
		if (linkedHashMap.get("accCode")!=null&&linkedHashMap.get("userNickname")!=null) {
			linkedHashMap.put("userNickname",PeiniUtils.updateNickName(linkedHashMap.get("userNickname").toString(), linkedHashMap.get("accCode").toString()));
			vo.setToUserNickname(PeiniUtils.updateNickName(linkedHashMap.get("userNickname").toString(), linkedHashMap.get("accCode").toString()));
		}
		vo.setUserId(mapUser);
		if (linkedHashMap.get("toaccCode")!=null&&linkedHashMap.get("toUserNickname")!=null) {
			linkedHashMap.put("toUserNickname",PeiniUtils.updateNickName(linkedHashMap.get("toUserNickname").toString(), linkedHashMap.get("toaccCode").toString()));
		}
		RedisUtil.hset((RedisConstants.SQUARE_COMMT+squareId).getBytes(), linkedHashMap.get("id").toString().getBytes(), SerializeUtil.serialize(vo));
	}
}
